<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Gregwar\Captcha\CaptchaBuilder;
use Session;
use Crypt;
use DB;
class LoginController extends Controller
{
    //前台登录页面
    public function login()
    {
    	$title = '登录';
        $conf = DB::table('bro_config') -> first();
        $link = DB::table('bro_link') -> get();
    	return  view('home.login.login',['title' => $title,'link' => $link,'conf' => $conf]);
    }

    //生成验证码
    public function createcode($id)
    {
    	//生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder;
        //可以设置图片宽高及字体
        $builder->build($width = 100, $height = 40, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();

        //把内容存入session
        Session::flash('code', $phrase);
        //生成图片
        header("Cache-Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $builder->output();
    }

    //处理登录方法
    public function dologin(Request $request)
    {
    	//去除隐藏_token
    	$data = $request -> except('_token');

    	//判断验证码
    	$code = $request -> input('code');

    	if($code != session('code'))
    	{
    		return back() -> with(['error' => '验证码错误']);
    	}

    	//判断登录的用户名和密码
    	 $this -> validate($request,[
            'username' => 'required',
            'password' => 'required',

        ],[

            'username.required' => '用户名不能为空',
            'password.required' => '密码不能为空',
        ]);

    	//检测用户名
    	$username = $request -> input('username');
        // dd($username);
    	$data= DB::table('bro_user') ->where('username',$username) -> first();
        session(['user' => $data]);
    	// dd($data);
    	//判断
    	if(!empty($data))
    	{
    		//检测密码
            $password = Crypt::decrypt($data -> password);
            // dd($password);
            $pass = $request -> input('password');
            
            //判断
            if($pass == $password)
            {
                $status = $data -> status;
                if($status != 0)
                {
                    
                    $ltime = $data -> ltime = time();
                    // dd($ltime);
                    //记录登录时间
                    // dd($username);
                    // dd($ltime);
                    $arr=['ltime'=>$ltime];
                    $res = DB::table('bro_user') -> where('username',$username) -> update($arr);
                    
                    if($res)
                    {
                        //跳转首页
                        return redirect('/');
                    }
                }else{

                     return back() -> with(['error' => '用户已被禁用']);
                }
                
            }else{

                return back() -> with(['error' => '密码错误']);
            }
    	}else{
        
            return back() -> with(['error' => '用户名错误']);
        }

    	//检测密码
    	$password = Crypt::decrypt($data -> password);
    	// dd($password);
    	$pass = $request -> input('password');
    	
    	//判断
    	if($pass != $password)
    	{
    		return back() -> with(['error' => '密码错误']);
    	}else{
            //跳转首页
            return view('welcome');
        }
        session(['user' => $data]);
    	
    }

    //退出
    public function quit()
    {
        session(['user' => null]);
        return redirect('/login');
    }
}
